﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Drawing;
using LevelEditor.Components;
using LevelEditor.Objects;
using LevelEditor.Objects.Tiles;
using LevelEditor.App;
using LevelEditor.Util;
using System.Xml;

namespace LevelEditor.App
{
    class Importer
    {
        public static LibraryItem Import(string imagePath, LevelEditor editor)
        {
            string tileType = "";
            FiletypeSelector fileTypeSelector = new FiletypeSelector();
            if (fileTypeSelector.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tileType = fileTypeSelector.FileType;
            }
            else return null;

            switch (Tile.ParseType(tileType))
            {
                case Tile.TileType.Character:
                    return ImportChar(imagePath, editor);
                case Tile.TileType.CommonTile:
                    return ImportCTile(imagePath, editor);
                case Tile.TileType.Item:
                    return ImportItem(imagePath, editor);
                case Tile.TileType.Door:
                    return ImportDoor(imagePath, editor);
            }

            return null;
        }

        public static LibraryItem ImportChar(string path, LevelEditor editor)
        {
            int startIndex = path.LastIndexOf("_") + 1;
            string tileName = path.Substring(startIndex, path.LastIndexOf(".") - startIndex);

            Character tile = new Character(editor);
            tile.Name = tileName;
            tile.myType = Tile.TileType.Character;
            tile.Image = new Bitmap(path);
            tile.Height = tile.Image.Height;
            tile.Width = tile.Image.Width;
            tile.ImageFormat = path.Substring(path.LastIndexOf(".") + 1).ToUpper(); // only the format
            tile.Location = new Point(300, 300);

            LibraryItem item = new LibraryItem(tile, editor);

            tile.Father = item;

            return item;
        }

        public static LibraryItem ImportItem(string path, LevelEditor editor)
        {
            int startIndex = path.LastIndexOf("_") + 1;
            string tileName = path.Substring(startIndex, path.LastIndexOf(".") - startIndex);

            Item tile = new Item(editor);
            tile.Name = tileName;
            tile.Image = new Bitmap(path);
            tile.Height = tile.Image.Height;
            tile.Width = tile.Image.Width;
            tile.ImageFormat = path.Substring(path.LastIndexOf(".") + 1).ToUpper(); // only the format
            tile.Location = new Point(300, 300);

            LibraryItem item = new LibraryItem(tile, editor);

            tile.Father = item;

            return item;
        }

        public static LibraryItem ImportCTile(string path, LevelEditor editor)
        {
            int startIndex = path.LastIndexOf("_") + 1;
            string tileName = path.Substring(startIndex, path.LastIndexOf(".") - startIndex);

            CTile tile = new CTile(editor);
            tile.Name = tileName;
            tile.Image = new Bitmap(path);
            tile.Height = tile.Image.Height;
            tile.Width = tile.Image.Width;
            tile.ImageFormat = path.Substring(path.LastIndexOf(".") + 1).ToUpper(); // only the format
            tile.Location = new Point(300, 300);

            LibraryItem item = new LibraryItem(tile, editor);

            tile.Father = item;

            return item;
        }

        public static LibraryItem ImportDoor(string path, LevelEditor editor)
        {
            int startIndex = path.LastIndexOf("_") + 1;
            string tileName = path.Substring(startIndex, path.LastIndexOf(".") - startIndex);

            Door tile = new Door(editor);
            tile.Name = tileName;
            tile.Image = new Bitmap(path);
            tile.Height = tile.Image.Height;
            tile.Width = tile.Image.Width;
            tile.ImageFormat = path.Substring(path.LastIndexOf(".") + 1).ToUpper(); // only the format
            tile.Location = new Point(300, 300);

            LibraryItem item = new LibraryItem(tile, editor);

            tile.Father = item;

            return item;
        }

    }
}
